package com.wenjiang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenjiang.common.dto.AddressParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.entity.MallUserAddress;
import com.wenjiang.mapper.MallUserAddressMapper;
import com.wenjiang.service.MallUserAddressService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwj
 * @since 2021-07-20
 */
@Service
public class MallUserAddressServiceImpl extends ServiceImpl<MallUserAddressMapper, MallUserAddress> implements MallUserAddressService {


    //根据id查询用户填写的地址
    @Override
    public Result getAddressById(Long userId) {

        List<MallUserAddress> mallUserAddress = baseMapper.selectList(new QueryWrapper<MallUserAddress>().eq("user_id", userId));

        return Result.succ(mallUserAddress);
    }

    @Override
    public Result saveOrUpdate(AddressParam addressParam, Long userId) {

        MallUserAddress mallUserAddress = new MallUserAddress();

        BeanUtil.copyProperties(addressParam, mallUserAddress);

        mallUserAddress.setUserId(userId);

        saveOrUpdate(mallUserAddress);

        return Result.succ("编辑成功");
    }

    @Override
    public Result del(Long addressId) {


        // 删除
        boolean b = removeById(addressId);

        if (b) {
            return Result.succ("删除成功");
        }
        return Result.fail("删除失败");


    }
}
